/*
 * Avatar.java - Created on May 28, 2007
 *
 * Copyright 2006,2007,2009 Matthieu Chase Heimer
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */
package com.macro10.xroads.entity;

import com.macro10.switchboard.entity.Account;
import com.macro10.switchboard.entity.SettingStore;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;

@NamedQueries({
    @NamedQuery(name = "findAvatarsByAccountId", query = "SELECT avatar FROM Avatar AS avatar WHERE avatar.account.lowerCaseClientId = :lowerCaseClientId ORDER BY avatar.lowerCaseAvatarId"),
    @NamedQuery(name = "findAvatarById", query = "SELECT avatar FROM Avatar AS avatar WHERE avatar.lowerCaseAvatarId = :lowerCaseAvatarId"),
    @NamedQuery(name = "findAllAvatars", query = "SELECT avatar FROM Avatar AS avatar ORDER BY avatar.lowerCaseAvatarId"),
    @NamedQuery(name = "findOnlineAvatars", query = "SELECT avatar FROM Avatar AS avatar WHERE avatar.isOnline = :isOnline ORDER BY avatar.lowerCaseAvatarId")
})
@Entity
public class Avatar extends SettingStore implements Comparable<Avatar> {

    @Column(unique = true, nullable = false)
    private String avatarId;
    @Column(unique = true, nullable = false)
    private String lowerCaseAvatarId;
    @Column(nullable = false)
    private boolean isOnline;
    @ManyToOne
    private Area area;
    @ManyToOne(optional = false)
    private Account account;

    public Avatar() {
    }

    public Avatar(Account account, String avatarId) {
        this.account = account;
        this.avatarId = avatarId;
        lowerCaseAvatarId = avatarId.toLowerCase();
    }

    public Account getAccount() {
        return account;
    }

    public void setAccount(Account account) {
        this.account = account;
    }

    public String getAvatarId() {
        return avatarId;
    }

    public void setAvatarId(String avatarId) {
        this.avatarId = avatarId;
        lowerCaseAvatarId = avatarId.toLowerCase();
    }

    public boolean isOnline() {
        return isOnline;
    }

    public void setOnline(boolean isOnline) {
        this.isOnline = isOnline;
    }

    public Area getArea() {
        return area;
    }

    public void setArea(Area area) {
        if (this.area != null) {
            this.area.getAvatars().remove(this);
        }
        this.area = area;
        area.getAvatars().add(this);
    }

    @Override
    public String toString() {
        return avatarId;
    }

    @Override
    public boolean equals(Object o) {
        if (o != null && o instanceof Avatar) {
            Avatar a = (Avatar) o;
            if (lowerCaseAvatarId.equals(a.lowerCaseAvatarId)) {
                return true;
            } else {
                return false;
            }
        } else {
            return false;
        }
    }

    @Override
    public int hashCode() {
        int hash = 7;
        hash = 37 * hash + (this.lowerCaseAvatarId != null ? this.lowerCaseAvatarId.hashCode() : 0);
        return hash;
    }

    public int compareTo(Avatar other) {
        return lowerCaseAvatarId.compareTo(other.lowerCaseAvatarId);
    }
}
